package com.future.controller;

import java.util.HashMap;
import java.util.Map;

import com.future.common.annotation.ControllerMapping;
import com.future.common.annotation.NoNeedLogin;
import com.future.common.base.BaseController;
import com.future.common.utils.IDUtil;
import com.future.dao.XjyDao;
import com.future.model.Xjy;
import com.future.service.AuthenticationService;
import com.jfinal.aop.Inject;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.wxaapp.api.WxaUserApi;
@ControllerMapping("/wxa")
@NoNeedLogin
public class WxaController extends BaseController{
	@Inject
	private WxaUserApi wxaUserApi;
	@Inject
	private XjyDao xjyDao;
	@Inject
	private AuthenticationService authenticationService;
	public void getWxaOpenid(String code){
		ApiResult apiResult = wxaUserApi.getSessionKey(code);
		String openid = apiResult.get("openid");
		Xjy xjy = this.xjyDao.findByOpenid(openid);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("openid", openid);
		map.put("xjy", xjy);
		renderResult(map);
	}
	
	public void bindUserWithOpenid(String name, String phone, String openid){
		Xjy xjy = this.xjyDao.findByNameAndPhone(name, phone);
		if(xjy == null){
			xjy = new Xjy();
			xjy.setXjyid(IDUtil.getId());
			xjy.setName(name);
			xjy.setPhone(phone);
			xjy.setOpenid(openid);
			xjy.save();
		}else{
			xjy.setOpenid(openid);
			xjy.update();
		}
		
		renderResult("xjr", xjy);
	}
}
